"""
面试状态检查管理命令
"""
from django.core.management.base import BaseCommand
from interviews.tasks import InterviewStatusManager


class Command(BaseCommand):
    """Django管理命令：检查面试状态"""
    
    help = '检查和更新面试状态'
    
    def add_arguments(self, parser):
        parser.add_argument(
            '--check-type',
            type=str,
            choices=['all', 'expired', 'reminders', 'cleanup'],
            default='all',
            help='指定检查类型'
        )
        
        parser.add_argument(
            '--dry-run',
            action='store_true',
            help='只显示将要执行的操作，不实际执行'
        )
    
    def handle(self, *args, **options):
        check_type = options['check_type']
        dry_run = options['dry_run']
        
        if dry_run:
            self.stdout.write(
                self.style.WARNING('这是一次试运行，不会实际修改数据')
            )
        
        self.stdout.write(f'开始执行面试状态检查: {check_type}')
        
        manager = InterviewStatusManager()
        
        try:
            if check_type == 'all':
                results = manager.run_all_checks()
            elif check_type == 'expired':
                results = {
                    'expired_applications': manager.check_expired_applications(),
                    'expired_interviews': manager.check_expired_interviews()
                }
            elif check_type == 'reminders':
                results = {
                    'reminders_sent': manager.send_interview_reminders()
                }
            elif check_type == 'cleanup':
                results = {
                    'cleaned_records': manager.cleanup_old_data()
                }
            
            # 显示结果
            self.stdout.write(
                self.style.SUCCESS('面试状态检查完成!')
            )
            
            for key, value in results.items():
                if key == 'error':
                    self.stdout.write(
                        self.style.ERROR(f'错误: {value}')
                    )
                else:
                    self.stdout.write(f'{key}: {value}')
                    
        except Exception as e:
            self.stdout.write(
                self.style.ERROR(f'执行失败: {str(e)}')
            )
            raise
